<?php

namespace App\Http\Controllers\Home;

use Illuminate\Support\ServiceProvider;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
use Crypt;
use Exception;
class OrderController extends Controller
{
    //
    public function receipt($oid)
    {   
        $status = 3;
        $res = DB::table('orders') -> where('oid',$oid) ->update(['status' => $status]);
        if($res)
        {
            return redirect('/home/userinfo/order');
        }else
        {
            return back() -> with(['info' => '订单']);
        }
    }
    public function delete($oid)
    {
        DB::beginTransaction();
        try
        {   
            $res = DB::table('orders') -> where('oid',$oid) -> delete();
            if(!$res)
            {
                throw new Exception("Error Processing Request", 1);
            }
            $res2 = DB::table('orders_info') -> where('oid',$oid) -> delete();
            if(!$res2)
            {
                throw new Exception("Error Processing Request", 2);
            }
            DB::commit();
           
            return redirect('/home/userinfo/order');
        }catch (\Exception $e){
            DB::rollback();
            echo $e -> getMessage();
            echo $e -> getCode();
            return back() -> with(['info' => '删除失败']);
        }
    }


    public function insert(Request $request)
    {   
        //先获取uid addrid oid 生成订单表
        DB::beginTransaction();
        try
        {
            $uid = session('master') -> id;
            // dd($uid);
            $addrid = DB::table('addr') -> where('uid','=',$uid) ->where('status','=','1') -> first();
            if(!$addrid)
            {
                throw new Exception("Error Processing Request", 1);
            }
            $oid = time().rand(10000,99999);       
            $order['oid'] = $oid;   
            $order['uid'] = $uid;
            $order['addrid'] = $addrid -> id;
            $order['status'] = 0;
            $time = Date('Y-m-d H:i:s');
            $order['created_at'] = $time;
            $order['updated_at'] = $time;

            $res01 = DB::table('orders') -> insert($order); 
            // dd($res01);
            if(!$res01)
            {
                throw new Exception("Error Processing Request", 2);
                
            }
            $data = DB::table('cart') 
                -> select('cart.*','goods.goodspic','goods.price','goodsname')
                -> leftJoin('goods','cart.gid','=','goods.gid')
                -> where('uid','=',$uid)
                -> get();
            $sum = 0;
            foreach ($data as $key => $value)
            {   
            // dd($value);
            $sum += $value -> price * $value -> scnt;
            $order_info['oid'] = $oid;
            $order_info['gid'] = $value -> gid;
            $order_info['scnt'] = $value -> scnt;
            $order_info['colorid'] = 1;
            $order_info['memoryid'] = 1;
            // dd($order_info);
            $res02 = DB::table('orders_info') -> insert($order_info);
            // dd($res02);
            if(!$res02)
            {
                throw new Exception("Error Processing Request", 3);
                
            }
            // dd($res);
            }
            Session::put('sum',$sum);
            Session::put('oid',$oid);
            $res03 = DB::table('cart') -> where('uid',$uid) -> delete();
            if(!$res03)
            {
                throw new Exception("Error Processing Request", 4);
                
            }
            DB::commit();
            return redirect('/home/order/finish');
        }catch (\Exception $e){
            DB::rollback();
            echo $e -> getMessage();
            echo $e -> getCode();
            return redirect('/home/cart/index');
        }
        
    }

    public function finish()
    {
        $oid = session('oid');
        $sum = session('sum');
        return view('home.order.finish',['oid' => $oid,'sum' => $sum]);
    }
    public function pay($oid)
    {   
        $status = 1;
        $res = DB::table('orders') -> where('oid',$oid) -> update(['status' => $status]);
        if($res)
        {
            
            return redirect('/home/order/pay_finish') ;
        }else
        {
            return back() -> with(['info' => '支付失败']);
        }
    }
    public function pay_finish()
    {

        return view('home.order.pay');
    }

    public function index(Request $request)
    {   
        $uid = session('master') -> id;
        // dd($uid);
        $data_addr = DB::table('addr') -> where('uid','=',$uid) -> get();
        $data = DB::table('cart') 
                -> select('cart.*','goods.goodspic','goods.price','goodsname')
                -> leftJoin('goods','cart.gid','=','goods.gid')
                -> where('uid','=',$uid)
                -> get();
        // dd($data_addr);
        // dd($data);

        if(!$data_addr)
        {
            return redirect('/home/userinfo') -> with(['into' => '请添加地址']);
        }
        $data= json_decode($data);
             // dd($data);
                  $sum = 0;
                  foreach ($data as $key => $value) {
                       
                       $sum += $value -> scnt * $value -> price;
                }
                    // dd($value);
                   
                
                Session::put('sum',$sum);
        return view('home.order.index',['title' => '确认订单','data_addr' => $data_addr,'data' => $data]);
    }
}